package com.lonely.api.mes.service;

import cn.hutool.core.convert.Convert;
import com.lonely.api.mes.model.MesApiResult;
import com.lonely.api.mes.model.dto.DomesticBulkGoodsPlaceOrderDTO;
import com.lonely.api.mes.model.dto.MesReportWorkDTO;
import com.lonely.api.mes.model.dto.SynchronizedStyleDTO;
import com.lonely.api.mes.model.dto.SynchronizedUserDTO;
import com.lonely.api.mes.model.vo.MesReportWorkVO;
import com.lonely.api.mes.util.RestTemplateUtil;
import com.lonely.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author : 黄志标
 * @date: 2025/11/12 14:09
 * @description: 同步到mes 服务实现
 */
@Service
@Slf4j
public class SynchronizedMesServiceImpl implements ISynchronizedMesService {


    /**
     * 员工同步接口
     * 接口地址：http://101.132.244.100:31107/doc.html#/%E5%91%98%E5%B7%A5%E6%8E%A5%E5%8F%A3/%E5%91%98%E5%B7%A5%E5%90%8C%E6%AD%A5%E6%8E%A5%E5%8F%A3/addEmployeeUsingPOST
     *
     * @param synchronizedUserDTO
     * @return
     */
    @Override
    public void staffSynchronized(SynchronizedUserDTO synchronizedUserDTO) {
        String url = "http://101.132.244.100:31107/out/user/add/model1/3dbe5179-bd09-4999-a505-d1399e22b8c1";
        MesApiResult mesApiResult;
        try {
            mesApiResult = RestTemplateUtil.postForObject(url, synchronizedUserDTO, MesApiResult.class);
        } catch (Exception e) {
            log.error("同步员工信息失败,异常原因：{}", ExceptionUtils.getStackTrace(e));
            throw new BusinessException(String.format("同步员工信息失败,异常原因：%s", e.getMessage()));
        }
        if (Objects.equals(mesApiResult.getSt(), "0")) {
            log.info("同步员工信息成功..");
        } else {
            log.error("同步员工信息失败,返回结果：{}", mesApiResult);
            throw new BusinessException(String.format("同步员工信息失败,异常原因：%s", mesApiResult.getMsg()));
        }

    }

    /**
     * 款式同步接口
     * 接口地址：http://101.132.244.100:31107/doc.html#/%E6%AC%BE%E5%BC%8F%E6%8E%A5%E5%8F%A3/%E6%AC%BE%E5%BC%8F%E5%90%8C%E6%AD%A5/inputUsingPOST
     *
     * @param synchronizedStyleDTO
     */
    @Override
    public void styleSynchronized(SynchronizedStyleDTO synchronizedStyleDTO) {
        String url = "http://101.132.244.100:31107/out/style/create/model1/3dbe5179-bd09-4999-a505-d1399e22b8c1";
        MesApiResult mesApiResult;
        try {
            mesApiResult = RestTemplateUtil.postForObject(url, synchronizedStyleDTO, MesApiResult.class);
        } catch (Exception e) {
            log.error("同步款式信息失败,异常原因：{}", ExceptionUtils.getStackTrace(e));
            throw new BusinessException(String.format("同步款式信息失败,异常原因：%s", e.getMessage()));
        }
        if (Objects.equals(mesApiResult.getSt(), "0")) {
            log.info("同步款式信息成功..");
        } else {
            log.error("同步款式信息失败,返回结果：{}", mesApiResult);
            throw new BusinessException(String.format("同步款式信息失败,异常原因：%s", mesApiResult.getMsg()));
        }

    }

    /**
     * 国内大货下单接口
     * 接口地址：http://101.132.244.100:31107/doc.html#/%E8%AE%A2%E5%8D%95%E6%8E%A5%E5%8F%A3/%E5%A4%A7%E8%B4%A7%E5%8D%95%E6%8E%A5%E5%8F%A3/inputUsingPOST_2
     *
     * @param domesticBulkGoodsPlaceOrderDTO
     */
    @Override
    public void domesticBulkGoodsPlaceOrder(DomesticBulkGoodsPlaceOrderDTO domesticBulkGoodsPlaceOrderDTO) {
        String url = "http://101.132.244.100:31107/out/pp/create/model2/3dbe5179-bd09-4999-a505-d1399e22b8c1";
        MesApiResult mesApiResult;
        try {
            mesApiResult = RestTemplateUtil.postForObject(url, domesticBulkGoodsPlaceOrderDTO, MesApiResult.class);
        } catch (Exception e) {
            log.error("国内大货下单失败,异常原因：{}", ExceptionUtils.getStackTrace(e));
            throw new BusinessException(String.format("国内大货下单失败,异常原因：%s", e.getMessage()));
        }
        if (Objects.equals(mesApiResult.getSt(), "0")) {
            log.info("国内大货下单成功..");
        } else {
            log.error("国内大货下单失败,返回结果：{}", mesApiResult);
            throw new BusinessException(String.format("国内大货下单失败,异常原因：%s", mesApiResult.getMsg()));
        }

    }

    /**
     * 报工接口
     *
     * @param mesReportWorkDTO
     * @return
     */
    @Override
    public MesReportWorkVO reportWork(MesReportWorkDTO mesReportWorkDTO) {
        String url = "http://101.132.244.100:31107/pp/report/model2/3dbe5179-bd09-4999-a505-d1399e22b8c1";
        MesApiResult mesApiResult;
        try {
            mesApiResult = RestTemplateUtil.postForObject(url, mesReportWorkDTO, MesApiResult.class);
        } catch (Exception e) {
            log.error("获取报工接口失败,异常原因：{}", ExceptionUtils.getStackTrace(e));
            throw new BusinessException(String.format("获取报工接口失败,异常原因：%s", e.getMessage()));
        }
        if (Objects.equals(mesApiResult.getSt(), "0")) {
            log.info("获取报工接口成功..");

            // 提取内容
            if (mesApiResult.getBody() != null) {
                try {
                    MesReportWorkVO result = Convert.convert(MesReportWorkVO.class, mesApiResult.getBody());
                    return result;
                } catch (Exception e) {
                    log.error("提取报工接口响应数据异常，异常原因：{}", ExceptionUtils.getStackTrace(e));
                    throw new BusinessException(String.format("提取报工接口响应数据异常，异常原因：%s", e.getMessage()));
                }
            }else {
                return null;
            }
        } else {
            log.error("获取报工接口失败,返回结果：{}", mesApiResult);
            throw new BusinessException(String.format("获取报工接口失败,异常原因：%s", mesApiResult.getMsg()));
        }
    }
}
